<?php
class Pageley_Gateway_Mock implements Pageley_Gateway {
	
	private $types;
	private $ids;
    private $transaction;
	
	public function __construct() {
		$this->types = new Pageley_Util_ArrayMap();
		$this->ids = new Pageley_Util_ArrayMap();
	}
	
	public function getTransaction($existing = true, $singleStatement = false) {
		if($this->transaction === null) {
            $this->transaction = new Pageley_Gateway_TransactionMock($this);
        }
        return $this->transaction;
	}

    public function find($type, $id) {
    	$data = $this->getTypeData($type);
    	$result = null;
    	foreach($data as $entity) {
    		if($entity->getId() == $id) {
    			$result = $entity;
    			break;
    		}
    	}
    	return $result;
    }
    
    public function findByProperty($type, $propertyName, $value) {
        $query = new Pageley_Query_Basic($type, 
            new Pageley_Query_Condition_Equals($propertyName, $value));
        $results = $this->query($query);
        $result = null;
        if($results->size() > 1) {
            throw new Pageley_Gateway_Database_Exception("Expected zero or one results for search on property '" .
                $propertyName . "', found " . $results->size() . ".");
        }
        if($results->size() == 1) {
            $result = $results->get(0);
        }
        return $result;
    }

    public function query($query) {
    	$data = $this->getTypeData($query->getType());
        $results = new Pageley_Util_ArrayMap();
        foreach($data as $entity) {
            if($query->matches($entity)) {
                $results->add($entity);
            }
        }
        $order = $query->getOrder();
        if($order !== null) {
            $results->sort(new Pageley_Domain_EntityComparator($order));
        }
        $range = $query->getRange();
        if($range !== null) {
            $count = $results->size();
            $i = 0;
            while($i < $count) {
                if($i < $range->getStart() - 1 || $i > $range->getEnd() - 1) {
                    $results->remove($i);
                }
                $i++;
            }
        }
        return $results;
    }
    
    public function count($query) {
        return $this->query($query)->size();
    }

    public function insert($entity) {
    	$entity->setId($this->getNextId($entity->getType()));
    	$data = $this->getTypeData($entity->getType());
    	$data->add($entity);
    }

    public function update($entity) {
    	
    }

    public function delete($entity) {
    	$data = $this->getTypeData($entity->getType());
        $data->removeValue($entity);
    }
    
    private function getTypeData($type) {
    	$key = $type->getFullName();
    	$data = $this->types->get($key);
    	if($data === null) {
    		$data = new Pageley_Util_ArrayMap();
    		$this->types->put($key, $data);
    	}
    	return $data;
    }
    
    private function getNextId($type) {
    	$key = $type->getFullName();
        $id = $this->ids->get($key);
        if($id === null) {
        	$id = 1;
        } else {
        	$id++;
        }
        $this->ids->put($key, $id);
        return $id;
    }
    
    public function transactionComplete($transaction) {
    
    }
    
    public function generateId($subject) {
        return 1;
    }
}
?>